﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Streambolics.Notes.Gui
{
    public partial class ViewTree : UserControl
    {
        private int _Loading = 0;

        public ViewTree ()
        {
            InitializeComponent ();
        }

        public void BeginUpdate ()
        {
            tree.BeginUpdate ();
            _Loading++;
        }

        public void EndUpdate ()
        {
            tree.EndUpdate ();
            _Loading--;
        }

        public void AddViews (IEnumerable<View> aViews)
        {
            BeginUpdate ();
            try
            {
                foreach (View v in aViews)
                {
                    AddView (v);
                }
            }
            finally
            {
                EndUpdate ();
            }
        }

        private TreeNode AccessNode (TreeNode aParent, string aName)
        {
            TreeNodeCollection nodes;

            if (aParent == null)
            {
                nodes = tree.Nodes;
            }
            else
            {
                nodes = aParent.Nodes;
            }

            if (nodes.ContainsKey (aName))
            {
                return nodes[aName];
            }

            return nodes.Add (aName, aName);
        }

        public void AddView (View v)
        {
            TreeNode n = null;

            if (v.IsSpecial)
            {
                n = AccessNode (n, "Special Views");
            }

            foreach (string subName in v.Hierarchy)
            {
                n = AccessNode (n, subName);
            }

            if (n == null)
            {
                n = AccessNode (n, v.Name);
            }

            n.Tag = v;
        }

        public void SetDatabase (Database aDatabase)
        {
            BeginUpdate ();
            try
            {
                tree.Nodes.Clear ();
                AddViews (aDatabase.Views);        
            }
            finally
            {
                EndUpdate ();
            }
        }

        [Browsable (true)]
        public event EventHandler SelectionChanged;

        [Browsable (false)]
        public View Selected
        {
            get
            {
                if (tree.SelectedNode == null)
                {
                    return null;
                }
                else
                {
                    return (View)(tree.SelectedNode.Tag);
                }
            }
        }


        protected virtual void OnSelectionChanged (EventArgs e)
        {
            if (SelectionChanged != null)
            {
                SelectionChanged.Invoke (this, e);
            }
        }

        protected void DoSelectionChanged ()
        {
            OnSelectionChanged (EventArgs.Empty);
        }


        private void tree_AfterSelect (object sender, TreeViewEventArgs e)
        {
            if (_Loading == 0)
            {
                DoSelectionChanged ();
            }
        }


    }
}
